package bktech.soict.hust.bknote.activity.sms;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.CheckBox;
import android.widget.ListView;
import bktech.soict.hust.bknote.R;

public class ContactView extends Activity{

    private ListView mContactList;
    private boolean mShowInvisible;
    private CheckBox mShowInvisibleControl;
    
	protected void onCreate(Bundle savedInstanceState) {
	      super.onCreate(savedInstanceState);
	      setContentView(R.layout.contact_list);
	      mContactList = (ListView) findViewById(R.id.list);
	        mShowInvisibleControl = (CheckBox) findViewById(R.id.showInvisible);

	        // Initialize class properties
	        mShowInvisible = false;
	        mShowInvisibleControl.setChecked(mShowInvisible);

	        mContactList = (ListView) findViewById(R.id.list);

	        // Populate the contact list
	        populateContactList();
	}
	   private void populateContactList() {
	        // Build adapter with contact entries
	        Cursor cursor = getContacts();
	        
	        String[] from = new String[] {
	        		ContactsContract.Data.TIMES_CONTACTED,
	        		ContactsContract.Contacts.DISPLAY_NAME
	        };
	        int[] to = {
	        		R.id.contactName,
	        		R.id.contactID
	        };
	        
	        ContactPhotoAdapter cpa = new ContactPhotoAdapter(this, R.layout.contact_entry,
	        		cursor, from, to);
	        mContactList.setAdapter(cpa);
	    }
	private Cursor getContacts() {
        // Run query
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Data.TIMES_CONTACTED,
                ContactsContract.Contacts.HAS_PHONE_NUMBER
        };
        String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +
                (mShowInvisible ? "0" : "1") + "'";
        String[] selectionArgs = null;
        String sortOrder = ContactsContract.Data.TIMES_CONTACTED + " DESC, " +
        		ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

        return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
	}
	
}
